package list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

public class Demo06 {
    public static void main(String[] args) {
        /*
         * 在集合遍历期间更新集合
         */
        //ArrayList<String> list = new ArrayList<>();
        //List<String> list = Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        //Vector<String> list = new Vector<>();
        list.add("Tom");
        list.add("Jerry");
        list.add("Andy");
        list.add("Wang");
        list.add("Lee");
        Thread t1 = new Thread(){
            @Override
            public void run() {
                try {
                    sleep(200);
                    list.add(0,"熊大");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t1.start();
        for (String str:list) {
            System.out.println(str);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(list);
    }
}
